#!/bin/sh
### BEGIN INIT INFO
# Provides:          django
# Required-Start:    networking
# Required-Stop:     networking
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start the CIA Django server
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin

USER=cia
export HOME=/home/$USER

CIA_ROOT=$HOME/cia
DAEMON=$CIA_ROOT/manage.py
NAME=django
DESC="web server"
PIDFILE=/var/run/cia/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

DAEMON_OPTS="runfcgi socket=/var/run/cia/django.sock pidfile=$PIDFILE"

test -x $DAEMON || exit 0

set -e

. /lib/lsb/init-functions

case "$1" in
  start)
	log_begin_msg "Starting $DESC" $NAME
	if ! start-stop-daemon --start --quiet\
	--pidfile $PIDFILE --chuid $USER --exec $DAEMON -- $DAEMON_OPTS ; then
            log_end_msg 1
	else
            log_end_msg 0
	fi
    ;;
  stop)
	log_begin_msg "Stopping $DESC" $NAME
	if start-stop-daemon --quiet --stop --oknodo --retry 30\
	--pidfile $PIDFILE --user $USER; then
	    rm -f $PIDFILE
	    log_end_msg 0
	else
	    log_end_msg 1
	fi
	;;
  restart|force-reload)
	$0 stop
	sleep 1
	$0 start
	;;
  *)
	echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
	exit 1
	;;
esac

exit 0
